package com.fc.mall.service.impl;

import com.fc.mall.dao.SmsBargainDao;
import com.fc.mall.dto.*;
import com.fc.mall.dto.SmsBargainParam;
import com.fc.mall.dto.SmsBargainProdRequest;
import com.fc.mall.dto.SmsBargainProductDto;
import com.fc.mall.dto.SmsBargainProductResponse;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.OmsBargainMapper;
import com.fc.mall.mapper.PmsProductBargainMapper;
import com.fc.mall.mapper.PmsProductMapper;
import com.fc.mall.model.*;
import com.fc.mall.model.OmsBargainExample;
import com.fc.mall.model.PmsProduct;
import com.fc.mall.model.PmsProductBargain;
import com.fc.mall.model.PmsProductBargainExample;
import com.fc.mall.service.SmsBargainService;
import com.fc.mall.util.DateTimeHelper;
import com.github.pagehelper.PageHelper;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.List;

/**
 * Created by sunny on 2019/9/24.
 */

@Service
public class SmsBargainServiceImpl implements SmsBargainService {

    @Autowired
    private SmsBargainDao smsBargainDao;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private PmsProductBargainMapper productBargainMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Autowired
    private OmsBargainMapper omsBargainMapper;


    @Override
    public List<SmsBargainProductDto> queryProductBargainList(SmsBargainParam smsBargainParam, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return smsBargainDao.queryProductBargainList(smsBargainParam);
    }

    @Override
    public int createOrUpdateBargainProduct(SmsBargainProdRequest bargainProdRequest){
        //判断该商品是否在砍价中
        PmsProductBargainExample bargainExample = new PmsProductBargainExample();
        bargainExample.createCriteria().andProductIdEqualTo(bargainProdRequest.getProductId());
        List<PmsProductBargain> productBargainList = productBargainMapper.selectByExample(bargainExample);
        if (!productBargainList.isEmpty()){
            throw new ServiceException("该商品正在砍价中，请重新选择！");
        }

        //开始时间或结束时间存在空
        if (StringUtils.isEmpty(bargainProdRequest.getEffectTimeStr()) ||
                StringUtils.isEmpty(bargainProdRequest.getExpiredTimeStr())){
            throw new ServiceException("开始时间、结束时间不可为空！");
        }
        long startTime = DateTimeHelper.getDateFromString(bargainProdRequest.getEffectTimeStr(),DateTimeHelper.SQL_DATETIME).getTime();
        long endTime = DateTimeHelper.getDateFromString(bargainProdRequest.getExpiredTimeStr(),DateTimeHelper.SQL_DATETIME).getTime();
        long now = System.currentTimeMillis();
        if (endTime <= startTime || endTime <= now) {
            throw new ServiceException("结束时间应大于开始时间和当前时间！");
        }


        PmsProductBargain productBargain;
        if(null == bargainProdRequest.getId())
        {
            productBargain = new PmsProductBargain();
            mapperFacade.map(bargainProdRequest, productBargain);
            formateProductBargainTime(productBargain,bargainProdRequest.getEffectTimeStr(), bargainProdRequest.getExpiredTimeStr());
            return productBargainMapper.insertSelective(productBargain);
        }
        else
        {
            productBargain = productBargainMapper.selectByPrimaryKey(bargainProdRequest.getId());
            if(null == productBargain)
            {
                throw new ServiceException("当前砍价商品不存在");
            }
            mapperFacade.map(bargainProdRequest, productBargain);
            formateProductBargainTime(productBargain,bargainProdRequest.getEffectTimeStr(), bargainProdRequest.getExpiredTimeStr());
            return productBargainMapper.updateByPrimaryKeySelective(productBargain);
        }
    }

    @Override
    public SmsBargainProductResponse getBargainProductById(Long bargainId) {

        PmsProductBargain productBargain = productBargainMapper.selectByPrimaryKey(bargainId);
        if(null == productBargain)
        {
            throw new ServiceException("砍价商品不存在");
        }

        SmsBargainProductResponse response = new SmsBargainProductResponse();
        mapperFacade.map(productBargain, response);
        response.setEffectTimeStr(DateTimeHelper.getEpochTimeToString(productBargain.getEffectTime(),
                DateTimeHelper.SQL_DATETIME));
        response.setExpiredTimeStr(DateTimeHelper.getEpochTimeToString(productBargain.getExpiredTime(),
                DateTimeHelper.SQL_DATETIME));
        PmsProduct product = pmsProductMapper.selectByPrimaryKey(productBargain.getProductId());
        response.setSelectedProdName(product.getName());
        response.setSalePrice(product.getPrice());
        return response;
    }

    @Override
    public List<SmsBargainProductDto> getBargainProductByIds(SmsBargainParam smsBargainParam,int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<SmsBargainProductDto> dtos =  smsBargainDao.queryProductBargainList(smsBargainParam);
        return dtos;
    }

    @Override
    public List<SmsBargainProductDto> getBargainProductExcludeIds(SmsBargainParam smsBargainParam,int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<SmsBargainProductDto> dtos =  smsBargainDao.queryProductBargainListExclude(smsBargainParam);
        return dtos;
    }

    @Override
    public int delBargainProductId(Long bargainId) {
        OmsBargainExample bargainExample = new OmsBargainExample();
        bargainExample.createCriteria().andProductBargainIdEqualTo(bargainId);
        int count = omsBargainMapper.countByExample(bargainExample);
        if(count > 0)
        {
            throw new ServiceException("已发起砍价不可删除");
        }
        return productBargainMapper.deleteByPrimaryKey(bargainId);
    }

    private void formateProductBargainTime(PmsProductBargain productBargain, String effectTime, String expireTime)
    {
        try {
            productBargain.setEffectTime(DateTimeHelper.getEpochTimeFromString(effectTime));
            productBargain.setExpiredTime(DateTimeHelper.getEpochTimeFromString(expireTime));
        } catch (ParseException e) {
            throw new ServiceException(e.getMessage());
        }
    }

}
